package com.walk.zworld.ui.activity.my

import android.content.Intent
import android.os.Build
import android.text.Html
import android.view.View
import android.widget.ImageView
import android.widget.TextView
import androidx.annotation.RequiresApi
import androidx.lifecycle.ViewModelProvider
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.module.LoadMoreModule
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.walk.zworld.R
import com.walk.zworld.mvvm.Constant
import com.walk.zworld.mvvm.viewModel.CommonViewModel
import com.walk.zworld.mvvm.vm.ShareActivityViewModel
import com.walk.zworld.ui.activity.LoginActivity
import com.walk.zworld.ui.activity.ShareActivity
import com.walk.zworld.utils.FileUtils.Companion.load
import com.walk.zworld.view.SwipeItemLayout
import com.walk.zworld.webView.WebViewActivity
import com.base.lib.base.utils.MyMMKV
import com.base.lib.base.view.BaseMvvmListActivity
import com.base.lib.httpUtils.Article

class MyShareActivity : BaseMvvmListActivity<Article, ShareActivityViewModel, MyShareActivity.ShareAdapter>() {

    companion object {
        const val SHARE_SUCCESS = 1
    }
    private val commonVM: CommonViewModel by lazy { ViewModelProvider(this).get(CommonViewModel::class.java) }

    override fun onBindAdapter(): Class<ShareAdapter> = ShareAdapter::class.java

    override fun onBindViewModel(): Class<ShareActivityViewModel> = ShareActivityViewModel::class.java

    override fun initView() {
        mRecyclerView.run {
            addOnItemTouchListener(SwipeItemLayout.OnSwipeItemTouchListener(this@MyShareActivity))
        }
        mAdapter.run {
            setOnItemClickListener { adapter, view, position ->
                if (data.size != 0) {
                    val data = data[position]
                    WebViewActivity.start(this@MyShareActivity, data.id, data.title, data.link)
                }
            }
            addChildClickViewIds(R.id.iv_like, R.id.btn_delete, R.id.rl_content)
            setOnItemChildClickListener { adapter, view, position ->
                if (data.size == 0) return@setOnItemChildClickListener
                val res = data[position]
                when (view.id) {
                    R.id.iv_like -> {
                        if (!MyMMKV.mmkv.decodeBool(Constant.IS_LOGIN, false)) {
                            startActivity(Intent(this@MyShareActivity, LoginActivity::class.java))
                            return@setOnItemChildClickListener
                        }
                        val collect = res.collect
                        res.collect = !collect
                        setData(position, res)
                        if (collect) commonVM.cancelCollectArticle(res.id)
                            .observe(this@MyShareActivity, {})
                        else commonVM.addCollectArticle(res.id).observe(this@MyShareActivity, {})
                    }
                    R.id.btn_delete -> {
                        mViewModel.deleteShareArticle(res.id).observe(this@MyShareActivity) {
                            mAdapter.removeAt(position)
                        }
                    }
                    R.id.rl_content -> {
                        WebViewActivity.start(this@MyShareActivity, res.id, res.title, res.link)
                    }
                }
            }
        }
    }

    @RequiresApi(Build.VERSION_CODES.LOLLIPOP)
    override fun initData() {
        setTop("我的分享")
        setRight(R.drawable.add){
            if (MyMMKV.mmkv.decodeBool(Constant.IS_LOGIN, false)) {
                startActivityForResult(Intent(this, ShareActivity::class.java), SHARE_SUCCESS)
            } else {
                startActivity(Intent(this, LoginActivity::class.java))
            }
        }

    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        when (requestCode) {
            SHARE_SUCCESS -> {
                if (resultCode == RESULT_OK) initData()
            }
        }
    }

    class ShareAdapter: BaseQuickAdapter<Article, BaseViewHolder>(R.layout.item_share_list),
        LoadMoreModule {

        override fun convert(holder: BaseViewHolder, item: Article) {
            val authorStr = if (item.author.isNotEmpty()) item.author else item.shareUser
            holder.setText(R.id.tv_article_title, Html.fromHtml(item.title))
                .setText(R.id.tv_article_author, authorStr)
                .setText(R.id.tv_article_date, item.niceDate)
                .setImageResource(R.id.iv_like, if (item.collect) R.mipmap.ic_like else R.mipmap.ic_not_like)
            val chapterName = when {
                item.superChapterName.isNotEmpty() and item.chapterName.isNotEmpty() ->
                    "${item.superChapterName} - ${item.chapterName}"
                item.superChapterName.isNotEmpty() -> item.superChapterName
                item.chapterName.isNotEmpty() -> item.chapterName
                else -> ""
            }
            holder.setText(R.id.tv_article_chapterName, chapterName)
            if (item.envelopePic.isNotEmpty()) {
                holder.getView<ImageView>(R.id.iv_article_thumbnail)
                    .visibility = View.VISIBLE
                context.let {
                    load(it, item.envelopePic, holder.getView(R.id.iv_article_thumbnail))
                }
            } else {
                holder.getView<ImageView>(R.id.iv_article_thumbnail)
                    .visibility = View.GONE
            }
            val tv_fresh = holder.getView<TextView>(R.id.tv_article_fresh)
            if (item.fresh) {
                tv_fresh.visibility = View.VISIBLE
            } else {
                tv_fresh.visibility = View.GONE
            }
            val tv_top = holder.getView<TextView>(R.id.tv_article_top)
            if (item.top == "1") {
                tv_top.visibility = View.VISIBLE
            } else {
                tv_top.visibility = View.GONE
            }
            val tv_article_tag = holder.getView<TextView>(R.id.tv_article_tag)
            if (item.tags.size > 0) {
                tv_article_tag.visibility = View.VISIBLE
                tv_article_tag.text = item.tags[0].name
            } else {
                tv_article_tag.visibility = View.GONE
            }
            val tv_article_audit = holder.getView<TextView>(R.id.tv_article_audit)
            if (item.audit == 0) {
                tv_article_audit.visibility = View.VISIBLE
                tv_article_audit.text = context.getString(R.string.audited)
            } else {
                tv_article_audit.visibility = View.GONE
            }
        }
    }
}